<?php

require_once 'common/conn.php';

if (isset($_GET['nickname'])) {
    $nickname = $_GET['nickname'];
}

if (isset($_GET['email'])) {
    $email = $_GET['email'];
}

$db = new DBC();
if (isset($nickname)) {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold FROM t_device WHERE nickname = '" . $nickname . "'";
} else if (isset($email)) {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold FROM t_device WHERE email = '" . $email . "'";
}

if (isset($sql)) {
    $data = $db->fetchData($sql);
    if ($data != false && count($data) > 0) {
        $usercode = $data[0]['usercode'];
        $sql = "SELECT usercode,SUM(amount) AS amount FROM t_gold_record WHERE usercode = '" . $usercode . "'";
        $gold = $db->fetchData($sql);
        if ($gold != false && count($gold) > 0) {
            $amount = $gold[0]['amount'];        
            $sql = "UPDATE t_device SET gold = " . $amount . " WHERE devicecode = '" . $usercode . "'";
            $db->execute($sql);
        }
    }
}
if (isset($nickname)) {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold FROM t_device WHERE nickname = '" . $nickname . "'";
} else if (isset($email)) {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold FROM t_device WHERE email = '" . $email . "'";
}
$data_result = $db->fetchData($sql);
echo json_encode($data_result);
?>
